import React from 'react';

import dispatcher from 'lib/dispatcher';

import {restPostApi} from '../apis/core-util';

export default class HealthExaminationScope {
  constructor(container) {
    this.container = container;

    this.$state = {
      healthDataSource: {
        loading: true
      }
    };

    this.healthApi = restPostApi({
      __url: 'overview/ranking/'+ BlueWare.appId + '/performance',
      appVersionId: ()=>BlueWare.appVersionId,
      dateType:()=>this.container.dateType
    });
  }

  $load() {
    if (this.$state.healthDataSource.data) {
      return;
    }

    this.healthApi.fetch().done((data,params)=>{
      this.$state.healthDataSource={data};
      this.container.emit(()=>dispatcher.dispatch({$type:'web-view-resize-health'}));
    }).fail(error=>{
      this.$state.healthDataSource={error};
      this.container.emit();
    }).progress(()=>{
      this.$state.healthDataSource = {loading: true};

      this.container.emit();
    });
  }
}
